package com.dudu.utils;

import java.io.File;

import org.apache.log4j.Logger;

import com.dudu.service.WavTTSSynthesizeCallback;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SpeechUtility;
import com.iflytek.cloud.speech.SynthesizeToUriListener;

/**
 * 科大讯飞文字转语音工具类
 * 
 * @author zx
 *
 */
public class TTSUtils {

	private static Logger log = Logger.getLogger(TTSUtils.class);

	/*
	 * 語音合成器
	 */
	private static SpeechSynthesizer synthesizer;

	/*
	 * 录音文件后缀名
	 */
	public static final String FILE_EXTENSION = ".pcm";

	/*
	 * 最大超时时间
	 */
	private static final long MAX_TIMEOUT = 15 * 1000;

	static {
		String appId = DictionaryUtils.getValueByTypeAndKey("iflytek", "appid");
		SpeechUtility.createUtility(SpeechConstant.APPID + "=" + appId);// 科大讯飞
		synthesizer = SpeechSynthesizer.createSynthesizer();
		synthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");// 设置发音人
		synthesizer.setParameter(SpeechConstant.SPEED, "40");// 设置语速,数字越大语速越快
		synthesizer.setParameter(SpeechConstant.PITCH, "50");// 设置语调，范围 0~100
		synthesizer.setParameter(SpeechConstant.VOLUME, "80");// 设置音量，范围 0~100
	}

	public static void textToSpeech(String text, String path, AbstractTTSSynthesizeCallback callback, long timeout) {
		if (synthesizer != null) {
			synthesizer.setParameter("tte", "");
			long startTime = System.currentTimeMillis();
			synthesizer.synthesizeToUri(text, path + FILE_EXTENSION, new TTSSynthesizeToUriListener(callback));
			while (!callback.isDone()) {
				if ((System.currentTimeMillis() - startTime) > timeout) {
					callback.overtime();
					break;
				}
			}
		}
	}

	/**
	 * 文字转语音实现，默认超时时间为10秒
	 * 
	 * @param text
	 *            需要转换的文本
	 * @param path
	 *            语音文件生成路径
	 * @param callback
	 *            回调函数，由用户实现
	 */
	public static void textToSpeech(String text, String path, AbstractTTSSynthesizeCallback callback) {
		textToSpeech(text, path, callback, MAX_TIMEOUT);
	}

	static class TTSSynthesizeToUriListener implements SynthesizeToUriListener {

		private AbstractTTSSynthesizeCallback callback;

		public TTSSynthesizeToUriListener(AbstractTTSSynthesizeCallback callback) {
			this.callback = callback;
		}

		public void onBufferProgress(int progress) {
		}

		public void onSynthesizeCompleted(String uri, SpeechError error) {
			if (error != null) {
				log.error("语音文件转换失败", error);
				if (callback != null) {
					callback.doAfterTTSSynthesizeFailture(uri, error);
				}
			} else {
				if (callback != null) {
					callback.doAfterTTSSynthesize(uri);
				}
			}
			// 删除临时文件pcm文件
			File file = new File(uri);
			if (file.exists()) {
				file.delete();
			}
		}
	}

	public static void main(String[] args) {
		WavTTSSynthesizeCallback c = new WavTTSSynthesizeCallback();
		textToSpeech("张三，李四，王五", "D:\\Java\\Tomcat\\webapps\\voip_dudu\\../upload_voice/test1981/201611/201611171504243800", c);
		System.out.println(c.isOvertime());
	}

}
